#ifndef _CROSSENGINE_TIMER_H_
#define _CROSSENGINE_TIMER_H_

#include "list.h"
#include "portable.h"

namespace Cross
{

struct GTimerList;
class GlobalTimer;

typedef void (*GTimerFunc)(GlobalTimer*,GTimerList*,void*&);
typedef void (*GDeleteFunc)(void*);

class GlobalTimer
{
public:
  explicit GlobalTimer(uint32 gtick);
  GTimerList* CreateTimer(uint32 dt,GTimerFunc func,void* data,GDeleteFunc delFunc);
  void AddTimer(GTimerList* tl);
  void Tick(uint32 gtick);
  static void DestroyTimer(GTimerList* tl);
private:
  void OneTick();
  void Cascade(list_head* tmlst);
  void AddTimerImpl(GTimerList* tl);
  static const uint32 TM_BITS=8;
  static const uint32 TM_SIZE=1<<TM_BITS;
  static const uint32 TM_MASK=TM_SIZE-1;
  struct RotaryTbl
  {
    list_head entry[TM_SIZE];
    int idx;

    ~RotaryTbl();
    RotaryTbl();
  };
  RotaryTbl mRotaryTbls[4];
  uint32 mGTick;
  uint32 mLastTick;
};

}

#endif//_CROSSENGINE_TIMER_H_
